package com.zp.crm.service.RoidService.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zp.crm.dao.MenuDao.MenuDao;
import com.zp.crm.dao.RoidDao.RoidDao;
import com.zp.crm.dao.UserDao.UserDao;
import com.zp.crm.dto.Menu.MenuDto;
import com.zp.crm.dto.Roid.RoidDto;
import com.zp.crm.entity.Menu.MenuEntity;
import com.zp.crm.entity.Roid.RoidEntity;
import com.zp.crm.service.RoidService.RoidService;
import com.zp.crm.util.Tree.UiTree;
import com.zp.crm.util.targetLog.Log;
@Service("roidService")
public class RoidServiceImpl implements RoidService{

	@Resource
	public RoidDao roidDao;
	@Resource
	public UserDao userDao;
	@Resource
	public MenuDao menuDao;
	@Log(name="查询指定用户的权限，拥有及拥有的权限，并封装成span标签 ")
	@Override
	public Map<String, Object> UserRoidList(String code, String stuts) {
		Map<String, Object> map=new HashMap<>();
		String a="";
		String userroid=userDao.selectBykeyUserRoid(code);
		String[] obj=userroid.split(",");
		List<Integer> intlist=new ArrayList<>();
		for(int i=0;i<obj.length;i++){
			intlist.add(Integer.parseInt(obj[i]));
		}
		if(stuts.equals("已拥有")){
			List<RoidEntity> u=roidDao.selectBykeyUserRoidListIdName(intlist);
			a=new RoidDto().HtmlSpan(u);
		}
		if(stuts.endsWith("未拥有")){
			List<RoidEntity> t=roidDao.selectBykeyUserRoidListNotIdName(intlist);
			a=new RoidDto().HtmlSpan(t);
		}
		map.put("message", a);
		return map;
	}
	@Log(name="角色信息查询，可根据名称查询。 ")
	@Override
	public Map<String, Object> select(int page, int size,String name) {
		Map<String, Object> map=new HashMap<>();
		
		int PageIndex=(page-1)*size;
		
		int count=roidDao.selectcount(name);
		
		List<RoidEntity> list=roidDao.select(PageIndex,size,name);
		String a="";
		if(name != null){
			a=new RoidDto().ListHtml(count,list,page,1);
		}else{
			a=new RoidDto().ListHtml(count,list,page,0);
		}
		map.put("message", a);
		return map;
	}
	@Log(name="根据ID查询权限")
	@Override
	public Map<String, Object> RoidMenuList(int id) {
		Map<String, Object> map=new HashMap<>();

		String roid=roidDao.RoidMenuList(id);
		List<Integer> intlist=new ArrayList<>();
		if(roid != null){
			String[] obj =roid.split(",");
			for(int i=0;i<obj.length;i++){
				intlist.add(Integer.parseInt(obj[i]));
			}
		}
		
		if(intlist.size()!=0){
			
			List<MenuEntity> menulist=menuDao.selectByKeyInRoid(intlist);
			
			List<UiTree> uilist=new MenuDto().MenuTreeTest(menulist);
			
			map.put("success", true);
			
			map.put("message", uilist);
		
		}else{
			map.put("success", false);
			
			map.put("message", null);
		}
		return map;
	}
	@Log(name="修改角色信息权限 ")
	@Override
	public Map<String, Object> RoidEditSelectMenu(int id) {
		Map<String, Object> map=new HashMap<>();
		String roid=roidDao.RoidMenuList(id);
		List<MenuEntity> menulist=menuDao.select();
		List<UiTree> uilist=new MenuDto().MenuTreeTestCheck(menulist,roid);
		map.put("message", uilist);
		return map;
	}
	@Log(name="设置角色信息权限 ")
	@Override
	public Map<String, Object> edit(String roid,int id) {
		Map<String, Object> map=new HashMap<>();
		
		int r=roidDao.edit(roid,id);
		
		if(r>0){
			map.put("success", true);
			map.put("message", "角色权限设置成功");
		}else{
			map.put("success", false);
			map.put("message", "角色权限设置失败");
		}

		return map;
	}
	@Log(name="删除角色信息 ")
	@Override
	public Map<String, Object> delete(int id) {
		Map<String, Object> map=new HashMap<>();
		
		int r=roidDao.delete(id);
		
		if(r>0){
			
			map.put("success", true);
			map.put("message", "角色删除成功！");
		}else{
			map.put("success", false);
			map.put("message", "角色删除失败");
		}

		return map;
	}
	@Log(name="添加角色信息 ")
	@Override
	public Map<String, Object> insert(RoidEntity entity) {
		Map<String, Object> map=new HashMap<>();
		
		int r=roidDao.insert(entity);
		
		if(r>0){
			
			map.put("success", true);
			map.put("message", "角色添加成功！");
		}else{
			map.put("success", false);
			map.put("message", "角色添加失败");
		}
		return map;
	}
	
}
